package pers.cz.logtail.handler;

import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.cz.netty.NettyClient;
import pers.cz.netty.protocal.Message;
import pers.cz.netty.protocal.MsgType;
import pers.cz.logtail.util.FileUtils;

import java.io.File;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: 测试监听文件变化
 * @author: Cheng Zhi
 * @create: 2022-10-04 11:57
 **/
public class LogListenner implements FileAlterationListener {

    Logger logger = LoggerFactory.getLogger(LogListenner.class);

    Map<String, Long> fileLineRember;
    private NettyClient nettyClient;
    public LogListenner(Map<String, Long> fileLineRember, NettyClient nettyClient) {
        this.fileLineRember = fileLineRember;
        this.nettyClient = nettyClient;
    }

    @Override
    public void onStart(FileAlterationObserver fileAlterationObserver) {
        logger.debug("monitor start scan files..");
    }

    @Override
    public void onDirectoryCreate(File file) {
        String fileName = file.getName();
        logger.debug(fileName + " director created.");
    }

    @Override
    public void onDirectoryChange(File file) {

        String fileName = file.getName();
        logger.debug(fileName + " director created." );
    }

    @Override
    public void onDirectoryDelete(File file) {
        String fileName = file.getName();
        System.out.println();
        logger.debug(fileName + " director created.");
    }

    @Override
    public void onFileCreate(File file) {
        String fileName = file.getName();
        Long countLines = FileUtils.getFileLineCount(file);
        logger.debug(fileName + "  changed." + "内容总行数为：" + countLines);
        fileLineRember.put(fileName, countLines);
    }

    @Override
    public void onFileChange(File file) {
        String fileName = file.getName();
        Long newLines = FileUtils.getFileLineCount(file);
        long oldLines = 0;
        if (fileLineRember.get(fileName) == null) {
            logger.debug(fileName + "  该文件第一次变更,不做处理!");
            fileLineRember.put(fileName, newLines);
            return;
        } else {
            oldLines = fileLineRember.get(fileName);
            logger.debug(fileName + "  原文件总行数为：" + oldLines);
        }
        logger.debug(fileName + "  changed." + "变更后内容总行数为：" + newLines);
        fileLineRember.put(fileName, newLines);
        long lines = newLines - oldLines;
        if (lines > 0) {
            String s = FileUtils.readTailContent(file, lines);
            Message message = new Message();
            message.setMsgType(MsgType.DATA);
            //message.setContent(FileUtils.decode(s, "POST_GIRL"));
            message.setContent(s);
            nettyClient.send(message);
        }

    }

    @Override
    public void onFileDelete(File file) {

    }

    @Override
    public void onStop(FileAlterationObserver fileAlterationObserver) {

        logger.debug("monitor stop scanning..");
    }

}
